package toolx

import (
	"gitee.com/workits/pkgs/configx"
	"gitee.com/workits/pkgs/utilx"
)

// NewModule 添加模块
func NewModule(projDir, moduleName string) {
	// 规范模块名称
	moduleName = CamelCaseToUdnderscore(moduleName)
	// 查看项目组件
	folders, err := utilx.FsList(projDir+PS+"internal"+PS+"component", true)
	if err != nil {
		panic("🙅读取项目组件失败")
	}

	// 错误处理
	defer func() {
		if err := recover(); err != nil {
			if err == ErrExist {
				panic("🔔模块已存在")
			}

			// 回退
			_ = utilx.FsRemove(projDir+PS+"internal"+PS+"app"+PS+moduleName, true)
			for _, folder := range folders {
				switch folder {
				case "router": // http
					_ = utilx.FsRemove(projDir+PS+"internal"+PS+"component"+PS+"router"+PS+moduleName+"_router.go", true)
				case "rpcTodo": // rpc
				case "scheduleTodo": // schedule
				}
			}

			panic(err)
		}
	}()

	// 判断模块是否存在
	if utilx.FsIsExist("") {
		panic(ErrExist)
	}

	// 读取配置文件
	var wkcfg WkCfg
	if _, err := configx.UnmarshalFiles(projDir+PS+".workits", &wkcfg); err != nil {
		panic("🙅配置文件读取失败")
	}

	// 生成文件
	// ---- 创建目录
	if err := utilx.CreateMutiDir(projDir + PS + "internal" + PS + "app" + PS + moduleName + PS + "component"); err != nil {
		panic("🙅目录internal/app/module/component创建失败")
	}
	if err := utilx.CreateMutiDir(projDir + PS + "internal" + PS + "app" + PS + moduleName + PS + "entity"); err != nil {
		panic("🙅目录internal/app/module/entity创建失败")
	}
	if err := utilx.CreateMutiDir(projDir + PS + "internal" + PS + "app" + PS + moduleName + PS + "handler"); err != nil {
		panic("🙅目录internal/app/module/handler创建失败")
	}
	if err := utilx.CreateMutiDir(projDir + PS + "internal" + PS + "app" + PS + moduleName + PS + "model"); err != nil {
		panic("🙅目录internal/app/module/model创建失败")
	}
	// ---- 创建文件
	if err := utilx.CreateFile(projDir+PS+"internal"+PS+"app"+PS+moduleName+PS+"component"+PS+".gitkeep", nil); err != nil {
		panic("🙅文件internal/app/module/component/.gitkeep生成失败")
	}
	if err := utilx.CreateFile(projDir+PS+"internal"+PS+"app"+PS+moduleName+PS+"entity"+PS+".gitkeep", nil); err != nil {
		panic("🙅文件internal/app/module/entity/.gitkeep生成失败")
	}
	if err := utilx.CreateFile(projDir+PS+"internal"+PS+"app"+PS+moduleName+PS+"handler"+PS+".gitkeep", nil); err != nil {
		panic("🙅文件internal/app/module/handler/.gitkeep生成失败")
	}
	if err := utilx.CreateFile(projDir+PS+"internal"+PS+"app"+PS+moduleName+PS+"model"+PS+".gitkeep", nil); err != nil {
		panic("🙅文件internal/app/module/model/.gitkeep生成失败")
	}

	for _, folder := range folders {
		switch folder {
		case "router": // http
			// ---- 创建目录
			if err := utilx.CreateMutiDir(projDir + PS + "internal" + PS + "app" + PS + moduleName + PS + "component" + PS + "router"); err != nil {
				panic("🙅目录internal/app/module/component/router创建失败")
			}
			if err := utilx.CopyFile(projDir+PS+".workits"+PS+"templates"+PS+"router.go.tpl", projDir+PS+"internal"+PS+"app"+PS+moduleName+PS+"component"+PS+"router"+PS+"router.go", false); err != nil {
				panic("🙅文件internal/app/component/router/router.go生成失败")
			}
			// internal/component/router/module_router.go
			params := make(map[string]any)
			params["GoModule"] = wkcfg.GoModule
			params["ProjModule"] = moduleName
			params["ProjModuleUpperCamel"] = UderscoreToUpperCamelCase(moduleName)
			params["ProjModuleLowerCamel"] = UderscoreToLowerCamelCase(moduleName)
			ParseTemplate(projDir+PS+".workits"+PS+"templates"+PS+"router_register.go.tpl", projDir+PS+"internal"+PS+"component"+PS+"router"+PS+moduleName+"_router.go", params)
		case "rpcTodo": // rpc
		case "scheduleTodo": // schedule
		}
	}
}
